<?php  if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
/////////////////////////////////////////////////////////////////////////////
//
// SuperPHP - 快速的中文PHP框架
//
// @package		SuperPHP
// @author		hmw@superphp.org
// @copyright	Copyright (c) 2010, SuperPHP.ORG
// @license		http://www.superphp.org/user_guide/license.html
// @link		http://www.superphp.org
// @since		Version 1.0
//
// 许可协议请查看 http://www.SuperPHP.org/
//
/////////////////////////////////////////////////////////////////////////////

/**
 * Super_View 基础视图类
 * 该类可以在控制器中被调用，也可以在函数中调用
 */
class Super_View {
	/**
	 * smarty实例
	 */
	private $smarty = null;
	/**
	 * 模板是否已输出
	 */
	private $displayed = FALSE;

	/**
	 * 构造函数，进行Smarty类的实例化操作
     * 将T,superUrl两个函数注册到视图模型里
	 */
    public function __construct() {
        if(FALSE == $GLOBALS['SUPER']['view']['enabled'])return FALSE;
        if(FALSE != $GLOBALS['SUPER']['view']['auto_ob_start'])ob_start();
        $this->smarty = loadClass($GLOBALS['SUPER']['view']['engine_name'],null,$GLOBALS['SUPER']['view']['engine_path']);
        $configs = $GLOBALS['SUPER']['view']['config'];
        if( is_array($configs) ) {
            foreach( $configs as $key => $value ) {
                if( isset($this->smarty->{$key}) )$this->smarty->{$key} = $value;
            }
        }
        $this->_addViewFunc('T', array( $this, '__smarty_T'));
        $this->_addViewFunc('superUrl', array( $this, '__smarty_superUrl'));
    }

	/**
	 * 输出页面
	 * @param tplname 模板文件路径
	 */
    public function display($tplname) {
        $this->addfuncs();
        $this->displayed = TRUE;
        if($GLOBALS['SUPER']['view']['debugging'] && SUPER_DEBUG)$this->smarty->debugging = TRUE;
        $this->smarty->display($tplname);
    }

	/**
	 * 获取Smarty的实例
	 */
    public function getView() {
        $this->addfuncs();
        return $this->smarty;
    }
    
	/**
	 * 自动输出页面
	 * @param tplname 模板文件路径
	 */
    public function auto_display($tplname) {
        if( TRUE != $this->displayed &&
                FALSE != $GLOBALS['SUPER']['view']['auto_display'] &&
                TRUE == $this->smarty->template_exists($tplname)) {
            $this->display($tplname);
        }
    }

	/**
	 * 注册已挂靠的视图函数
	 */
    public function addfuncs() {
        if( is_array($GLOBALS['SUPER']["view_registered_functions"]) ) {
            foreach( $GLOBALS['SUPER']["view_registered_functions"] as $alias => $func ) {
                $this->smarty->register_function($alias, $func);
            }
        }
    }
    
	/**
	 * 辅助superUrl的函数，让superUrl可在模板中使用。
	 * @param params 传入的参数
	 */
    public function __smarty_superUrl($params) {
        $module = $GLOBALS['SUPER']['default_module'];
        $controller = $GLOBALS['SUPER']["default_controller"];
        $action = $GLOBALS['SUPER']["default_action"];
        $args = array();
        $anchor = null;
        foreach($params as $key => $param) {
            if($key == $GLOBALS['SUPER']['url_module']){
                $module = $param;
            }elseif( $key == $GLOBALS['SUPER']["url_controller"] ) {
                $controller = $param;
            }elseif( $key == $GLOBALS['SUPER']["url_action"] ) {
                $action = $param;
            }elseif( $key == 'anchor' ) {
                $anchor = $param;
            }else {
                $args[$key] = $param;
            }
        }
        return superUrl($module,$controller, $action, $args, $anchor);
    }
    
	/**
	 * 辅助T的函数，让T可在模板中使用。
	 * @param params 传入的参数
	 */
    public function __smarty_T($params) {
        return T($params['w']);
    }

    private function _addViewFunc($alias, $callback_function){
        return addViewFunc($alias, $callback_function);
    }
    
}